package net.aiotos.common.dto;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;


/**
 * 接收服务器JSON数据对象
 */
@Data
@NoArgsConstructor
public class ResultErrorDTO implements Serializable {
    private ErrorEnum code;
    private String msg;
    private String data;
    private static final long serialVersionUID = -3821339130160848360L;

    public static <T> T toJavaObject(String jsonStr, Class<T> tClass) {
        return JSONObject.parseObject(jsonStr, tClass);
    }

    public static <T> List<T> toJavaArray(String jsonStr, Class<T> tClass) {
        return JSONArray.parseArray(jsonStr, tClass);
    }

    /**
     * 将JSON字符转换成对象
     * @param jsonStr
     * @return
     */
    public static ResultErrorDTO toObject(String jsonStr) {
        return JSONObject.parseObject(jsonStr, ResultErrorDTO.class);
    }

    /**
     * 将JSON字符转换成列表对象
     * @param jsonStr
     * @return
     */
    public static List<ResultErrorDTO> toArray(String jsonStr) {
        try {
            return JSONArray.parseArray(jsonStr, ResultErrorDTO.class);
        } catch (Exception ex) {

            System.out.println(jsonStr);
            throw ex;
        }

    }


}
